//
//  GKZYExamineWebNavigationController.m
//  GKZY_CLASS
//
//  Created by chenziqiang on 15/12/8.
//  Copyright © 2015年 eenet. All rights reserved.
//

#import "GKZYExamineWebViewController.h"
#import "FontImageHelper.h"
#import "xxptmobile.h"

@interface GKZYExamineWebViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIView *navigationArea;
@property (nonatomic, strong) UILabel *navigationTitleLabel;
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation GKZYExamineWebViewController

-(instancetype)initWithURLPath:(NSString *)urlPath{
    if (self = [super init]) {
        self.urlPath = urlPath;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    /*
    UIImage *icon = [FontImageHelper imageWithIcon:@"\U0000F104" fontName:FONT_FONTAWESOME iconColor:[UIColor whiteColor] iconSize:25];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton addTarget:self action:@selector(backToShell) forControlEvents:UIControlEventTouchDown];
    [backButton setImage:icon forState:UIControlStateNormal];
    [self.navigationArea addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.navigationArea).multipliedBy(1.3);
        make.left.equalTo(self.navigationArea).offset(10);
    }];
    
    
    [self.view addSubview:self.navigationArea];
    [self.navigationArea mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@(64));
    }];
    */
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlPath]]];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        /*
        make.top.equalTo(self.navigationArea.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
        */
        make.top.bottom.left.right.equalTo(self.view);
    }];
}

- (void)backToShell{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIWebViewDelegate

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSString *tagetWord = @"close";
        
        NSString *clickURLString = [[request URL] description];
        NSRange tagetRange = [clickURLString rangeOfString:tagetWord];
        NSString *tagetString = [clickURLString substringFromIndex:tagetRange.location];//截取URL中目标内容
        
        if ([tagetString isEqualToString:tagetWord]) {
            [self backToShell];
        }
    }
    return YES;
}


#pragma mark - Getter For Property

-(NSString *)urlPath{
    if (!_urlPath) {
        _urlPath = @"http://www.eenet.com";
    }
    return _urlPath;
}

#pragma mark - Getter For UI

-(UIView *)navigationArea{
    if (!_navigationArea) {
        _navigationArea = ({
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor colorWithRed:67/255.0 green:128/255.0 blue:203/255.0 alpha:1];
            
            [view addSubview:self.navigationTitleLabel];
            [self.navigationTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(view);
                make.centerY.equalTo(view).multipliedBy(1.3);
            }];
            
            view;
        });
    }
    return _navigationArea;
}

-(UILabel *)navigationTitleLabel{
    if (!_navigationTitleLabel) {
        _navigationTitleLabel = ({
            UILabel *label = [UILabel new];
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont systemFontOfSize:18];
            label.text = @"考试";
            [label sizeToFit];
            label;
        });
    }
    return _navigationTitleLabel;
}

-(UIWebView *)webView{
    if (!_webView){
        _webView = ({
            UIWebView *webView = [UIWebView new];
            webView.delegate = self;
            webView;
        });
    }
    return _webView;
}

@end
